package com.abu.ossms.controller;

import com.abu.ossms.template.OssTemplate;
import com.abu.ossms.properties.OssProperties;
import com.abu.utils.Resp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("edu/oss")
@EnableConfigurationProperties(OssProperties.class)
public class OssController {

    @Autowired
    private OssTemplate ossTemplate;

    @PostMapping("upload")
    public Resp upload(MultipartFile file) {
        String uploadUrl = this.ossTemplate.upload(file);
        if (StringUtils.isBlank(uploadUrl)) {
            return Resp.error();
        }
        return Resp.ok().data("url", uploadUrl);
    }

}
